<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="icontList">
      <el-table-column label="图标" align="center">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.iconUrl"
            fit="contain"
            :preview-src-list="[scope.row.iconUrl]"
            style="width: 30px; height: 30px">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="网站标题" align="center" prop="tag"/>
      <el-table-column label="使用状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 1 ? 'primary' : 'danger'">
            {{ scope.row.status == 1 ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equ:icont:edit']"
          >修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改系统控制对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="网站图标" prop="iconUrl">
          <el-upload
            class="icon-uploader"
            action="#"
            :http-request="handleUpload"
            :show-file-list="false"
            :before-upload="beforeUpload">
            <img v-if="form.iconUrl" :src="form.iconUrl" class="icon">
            <i v-else class="el-icon-plus icon-uploader-icon"></i>
          </el-upload>
          <el-input v-model="form.iconUrl" placeholder="图标上传自动填充" readonly/>
        </el-form-item>
        <el-form-item label="网站标题" prop="tag">
          <el-input v-model="form.tag" placeholder="请输入网站标题"/>
        </el-form-item>
        <el-form-item label="使用状态" prop="status">
          <el-radio-group
            v-model="form.status">
            <el-radio label="1">启用</el-radio>
            <el-radio label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listIcont, getIcont, updateIcont, uploadIcon} from "@/api/equ/icont";

export default {
  name: "Icont",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 系统控制表格数据
      icontList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        iconUrl: [
          {required: true, message: "请上传图标", trigger: "blur"}
        ],
        tag: [
          {required: true, message: "网站标题不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询系统控制列表 */
    getList() {
      this.loading = true;
      listIcont().then(response => {
        this.icontList = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        iconId: null,
        iconUrl: null,
        tag: null,
        status: "0" // 默认为禁用
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const iconId = row.iconId || this.ids
      getIcont(iconId).then(response => {
        this.form = response.data;
        this.form.status = response.data.status.toString();
        this.open = true;
        this.title = "修改系统控制";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.iconId != null) {
            updateIcont(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              window.location.reload(true);
            });
          }
        }
      }, () => {
        this.$modal.msgError("修改失败");
      });
    },
    /** 上传图标文件 */
    handleUpload(param) {
      const formData = new FormData();
      formData.append("file", param.file);
      uploadIcon(formData).then(response => {
        this.form.iconUrl = response.url;
        this.$modal.msgSuccess("上传成功");
      }).catch(() => {
        this.$modal.msgError("上传失败");
      });
    }
    ,
    /** 上传前检查 */
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$modal.msgError('上传图标只能是 JPG/PNG 格式');
      }
      if (!isLt2M) {
        this.$modal.msgError('上传图标大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
};
</script>

<style scoped>
.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
}

.icon-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: border-color 0.2s ease;
}

.icon-uploader .el-upload:hover {
  border-color: #409EFF;
}

.icon-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}

.icon {
  width: 120px;
  height: 120px;
  display: block;
}
</style>
